package com.start.log.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.core.entity.StartLogEntity;
import com.start.core.service.StartLogService;
import com.start.security.annotations.DataPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/start/log")
@RegisterMenu(value = "系统日志", systemCode = "start:log", component = "views/start/log/index", path = "/log/index")
public class StartLogController {
    @Autowired
    private StartLogService startLogService;

    @GetMapping("/page")
    @DataPermission("views/start/log/index")
    public Result<?> page(Page page, StartLogEntity entity) {
        LambdaQueryWrapper<StartLogEntity> lambdaQuery = Wrappers.lambdaQuery(entity);
        lambdaQuery.orderByDesc(StartLogEntity::getCreateTime);
        return Result.ok(startLogService.page(page, lambdaQuery));
    }

    @PostMapping("/add")
    @PreAuthorize("@pas.hasPermission('start:log:add')")
    @RegisterPermission(value = "start:log:info:add", name = "查看")
    public Result<?> add(@RequestBody StartLogEntity entity) {
        startLogService.save(entity);
        return Result.ok();
    }

    @PutMapping("/updateById")
    @PreAuthorize("@pas.hasPermission('start:log:edit')")
    @RegisterPermission(value = "start:log:info:edit", name = "编辑")
    public Result<?> updateById(@RequestBody StartLogEntity entity) {
        startLogService.updateById(entity);
        return Result.ok();
    }

    @GetMapping("/getById/{id}")
    @PreAuthorize("@pas.hasPermission('start:log:info')")
    @RegisterPermission(value = "start:log:info:info", name = "查看")
    public Result<?> getById(@PathVariable("id") String id) {
        return Result.ok(startLogService.getById(id));
    }


    @DeleteMapping("deleteById/{id}")
    @PreAuthorize("@pas.hasPermission('start:log:delete')")
    @RegisterPermission(value = "start:log:info:delete", name = "查看")
    public Result<?> deleteById(@PathVariable("id") String id) {
        startLogService.removeById(id);
        return Result.ok();
    }
}
